<template>
  <a-modal
    title="选择客户"
    width="85%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="visible=false"
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">

          <a-col :md="8" :sm="24">
            <a-form-item>
              <a-input allowClear  v-model="queryParam.cCusName" placeholder="客户名称" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      :columns="columns"
      :scroll="{ y: scroll }"
      :rowKey="(row) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :customRow="loadRowleft"
      :rowSelection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
        type: type,
        getCheckboxProps: CheckboxProps,
        columnWidth:50
      }"
      :bordered="true"
      size="small"
    >
      <template slot="dataIndex" slot-scope="text, record, index">
        <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
      </template>
      <template slot="cCusType" slot-scope="text, record">
        <a-tag v-if="record.cCusType === 0" color="blue">潜在</a-tag>
        <a-tag v-else-if="record.cCusType === 1" color="green">正式</a-tag>
        <a-tag v-else-if="record.cCusType === 2" color="red">休眠</a-tag>
        <a-tag v-else-if="record.cCusType === 3" color="orange">待转正</a-tag>
      </template>
      </a-table
    >
  </a-modal>
</template>

<script>



export default {
  props: {
    type: { type: String, default: 'radio', required: false },
    id: { type: String, required: false },
    deptId: { type: String,  default: '' },
  },
  mounted() {
    // this.getDataList()
  },
  computed: {
    scroll() {
      // console.log()
      return Number(document.documentElement.clientHeight) * 0.45
    },
  },
  data() {
    return {
      data: [],
      qid: '',
      newDeptId:null,
      DeptId: null,
      DeptList:[],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], //设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true,
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize),
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns:[
        { title: '客户编码', dataIndex: 'cCusCode', align: 'center', },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left' },
  { title: '客户分组', dataIndex: 'GroupName',  align: 'center' },
  { title: '业务负责人', dataIndex: 'PrincipalName',  align: 'center' },
  { title: '收货单位', dataIndex: 'ReceivingUnit',  align: 'center' },
  { title: '收货人', dataIndex: 'Addressee',  align: 'center' },
  { title: '收货人电话', dataIndex: 'TelNumber',  align: 'center' },
  { title: '收货地址', dataIndex: 'Address',  align: 'center' },
  { title: '客户类型', dataIndex: 'cCusType', align: 'center',  scopedSlots: { customRender: 'cCusType' } },

],
      queryParam: {  cCusName:''},
      selectedRowKeys: [],
      selectedRows: [],
      visible: false,
    }
  },
  watch:{
    deptId:{
      immediate:true,
      deep:true,
      handler(val){
        console.log(val)
      this.newDeptId=val
      }
    },

  },
  methods: {
    loadRowleft (record, index) {
      return {
        on: {
          click: () => {
            // 记录为disabled则直接返回，默认为不可选
            if (record.disabled) return
            const type = this.type
            const key = record.Id || index
            let selectedRows = this.selectedRows
            let selectedRowKeys = this.selectedRowKeys
            if (type === 'radio') {
              selectedRowKeys = [key]
              selectedRows = [record]
            }
            if (!this.selectedRowKeys.includes(key)) {
              selectedRowKeys.push(key)
              selectedRows.push(record)
            } else {
              const index = this.selectedRowKeys.findIndex((skey) => skey === key)
              selectedRows.splice(index, 1)
              selectedRowKeys.splice(index, 1)
            }
            this.onSelectChange(selectedRowKeys, selectedRows)
          }
        }
      }
    },
    getDeptList () {
      this.$http.post('/Base_Manage/Base_Department/GetUserDepartmentTreeList', {}).then(res => {
        if (res.Success) {
          this.DeptList = res.Data || []
        } else {
          this.DeptList = []
        }
      })
    },
    CheckboxProps(record) {
      return {
        props: {
          disabled: record.UsageStatus === false,
        },
      }
    },

    reset() {
      this.queryParam.cCusName=''
      this.pagination.current = 1
      this.pagination.pageSize = 25
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
	        DeptId: this.DeptId,
          Search: this.queryParam,
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    openChoose(companyId) {
      this.selectedRowKeys = []
      this.selectedRows=[]
      this.getDeptList()
      this.DeptId = companyId
      this.reset()
      this.visible = true
    },
    handleChoose() {
      if(this.selectedRows.length>0){
        this.visible = false
      this.$emit('onChoose', this.selectedRows)
      }else{
        this.$message.error('请选择后确认')
      }

    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-table-row{
  cursor: pointer;
}
/deep/.ant-modal{
  top:10px
}
</style>